package com.zb.zeus.auth.utils;

import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

import java.nio.charset.StandardCharsets;

/**
 * @author fujianjian
 * @date 2021/8/19 14:48
 **/
public final class CryptoUtils {

    private static RSA RSA_INSTANCE;

    static {
        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDew/+XsMFNp8xy6Qb+DoezLKzQas39n066xNQequlW2Q3qYC7BP/me6QoVLNTwKAE1/Qw8AdWBDNE8dQYHnANWKrYbJFf+/pX47xf1IBiFarANfIIbBlIFJGegignvUIoQfwGyuPoRE4mSKBwdl92bDnJPxNOUeUMasvhe8RwbcQIDAQAB";
        String privateKey = "MIICdAIBADANBgkqhkiG9w0BAQEFAASCAl4wggJaAgEAAoGBAN7D/5ewwU2nzHLpBv4Oh7MsrNBqzf2fTrrE1B6q6VbZDepgLsE/+Z7pChUs1PAoATX9DDwB1YEM0Tx1BgecA1YqthskV/7+lfjvF/UgGIVqsA18ghsGUgUkZ6CKCe9QihB/AbK4+hETiZIoHB2X3ZsOck/E05R5Qxqy+F7xHBtxAgMBAAECfwzDLeGC6ZA0DNfd1zNhobn3f+8bhLGtxCpEceKlPWO8YQ+3HaFKCQMRrCzfuf4ITLQgdehPh+vc0B/76bHbPlWWFPUufDM6TRblkHo0NgXTcZL9EfR+yGc6aPXRmEeO1fq9lXnvue/2IYurMv/9fIwtRYxAtq29SSQ8xkWoXQECQQDw5J6jc5zqee+Pfe7qhTDDeNwisP5ancWJU6TrXolwsa5uV3IXsWtOAQ1OOA8Kbw2aC2wMy590m/UhDWSwKj7pAkEA7Lxa0G8s6HOXX9N+nex26rYG5dfqEzAzxxCxzymu2ANd0nfr6WIflctOKftLPx9ZJho9eUknSxfFKqfnEvHzSQJAbv6YTe32bl+yzyYSkMK//kFRBTcSa8RogWcmBUFxCJkGK1dlfZ9FNxLrA5B29bLHzKPYrGiiV0OJBiq6qyU8CQJBALblLiQqQD+2ThSyiPfpYFYlG18nWKhKbTY2J6ktQu+QakPYcmJ0dIJ9931nr9KsnWtainPRmJHjY2h5H4T7eEECQBHFa2AYXHn8whwJnPHcK3XbHFlH3EGrs5x2jmVaY5OhTGCo/uxwYgzkUT0ejEA/M6hp1qeiz/amFZY3fNfvthw=";
        RSA_INSTANCE = new RSA(AsymmetricAlgorithm.RSA_ECB_PKCS1.getValue(), privateKey, publicKey);
    }

    public static void setRSAInstance(String privateKey, String publicKey) {
        RSA_INSTANCE = new RSA(AsymmetricAlgorithm.RSA_ECB_PKCS1.getValue(), privateKey, publicKey);
    }

    /**
     * 解密
     * @param encrypt 密文
     * @return
     */
    public static String decrypt(String encrypt) {
        return RSA_INSTANCE.decryptStr(encrypt, KeyType.PublicKey, StandardCharsets.UTF_8);
    }

    /**
     * 加密
     * @param plainText 明文
     * @return
     */
    public static String encrypt(String plainText) {
        return RSA_INSTANCE.encryptBase64(plainText, StandardCharsets.UTF_8, KeyType.PrivateKey);
    }
}
